package com.bdqn.vueboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bdqn.vueboot.entity.*;
import com.bdqn.vueboot.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mail")
@CrossOrigin
public class MailController {
    //邮件
    @Resource
    MailService mailService;
    //邮件类型
    @Resource
    MailTypeService mailTypeService;
    //用户
    @Resource
    SysUserService sysUserService;
    @Resource
    DepartmentService departmentService;
    @Resource
    StaticService staticService;

    @GetMapping("/list")
    @CrossOrigin
    public Map list(
            @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "mailType", required = false, defaultValue = "0") Integer mailType,
            @RequestParam(value = "mailStartData", required = false, defaultValue = "") String mailStartData,
            @RequestParam(value = "mailStatic", required = false, defaultValue = "0") String mailStatic,
            @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        IPage<Mail> mailIPage = mailService.allmodifyListBiz(mailType, mailStartData, mailStatic, pageIndex, limit);
        Map map = new HashMap();
        map.put("mailIPage", mailIPage);
        List<MailType> mailTypeList = mailTypeService.allMailTypeBiz();
        map.put("mailTypeList", mailTypeList);
        List<SysUser> sysUserList = sysUserService.allSysUserBiz();
        map.put("sysUserList", sysUserList);
        List<Department> departmentList = departmentService.allDepartmentListBiz();
        map.put("departmentList", departmentList);
        List<Static> staticList = staticService.queryMailStatic();
        map.put("staticList", staticList);
        return map;
    }

    @PostMapping("/seva")
    @CrossOrigin
    public int seva(@RequestBody Mail mail) {
        //获取当前时间装换成字符串类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String str = sdf.format(date);
        mail.setMailStartData(str);
        int count = mailService.seva(mail);
        System.out.println(mail);
        return count;
    }

    @GetMapping("/delMail")
    @CrossOrigin
    public int delConferenceRoot(@RequestParam(value = "mailId") Long mailId, @RequestParam(value = "isDel") Long isDel) {
        int count = mailService.delIsDeleByMailId(mailId, isDel);
        System.out.println(count);
        return count;
    }

    @GetMapping("/audit")
    @CrossOrigin
    public int sevaAudit(@RequestParam(value = "mailStatic") String mailStatic, @RequestParam(value = "mailId") Long mailId) {
        return mailService.sevaAudit(mailStatic, mailId);
    }
}
